Repository navigation
perf(covariance): weighted moments_XX peak memory 128 MB -> 0.8 MB - #346
Merged
clonker merged 2 commits intoNov 14, 2026
Merged
Conversation
A weighted moments_XX call built two full weighted copies of the data: one in _sum to take column sums, one in _M2_dense for the second moment. _sum now uses weights.dot(X) for the weighted column sums. _M2_dense weights one block of 1024 frames at a time in a reused buffer and adds up the block products. Results agree to floating-point rounding. Peak traced allocation, 200000 x 80 float64 with weights, dense mode: 128.07 MB -> 0.78 MB. Unweighted calls are unchanged. Co-Authored-By: Aiden <aiden@weco.ai>
Codecov Report✅ All modified and coverable lines are covered by tests. Additional details and impacted files@@ Coverage Diff @@
## main #346 +/- ##
=======================================
Coverage 94.03% 94.03%
=======================================
Files 141 141
Lines 11199 11207 +8
=======================================
+ Hits 10531 10539 +8
Misses 668 668
Flags with carried forward coverage won't be shown. Click here to find out more. ☔ View full report in Codecov by Harness. 🚀 New features to boost your workflow:
|
clonker
approved these changes
Nov 13, 2026
clonker
left a comment
Member
There was a problem hiding this comment.
thanks! i didn't 100%ly like the tradeoff in the block size so I made it adaptive. sweet improvement otherwise :)
clonker
enabled auto-merge
November 13, 2026 11:29
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
TL;DR
moments_XXcalls indeeptime.covariance.util._sumnow gets weighted column sums fromweights.dot(X), and_M2_denseadds up the weighted product over blocks of 1024 frames.tracemalloc: 128.07 MB -> 0.78 MB, 99.4% lower.python -m pytest tests/covariance -q: 67 passed, including the 22 tests intest_moments.py.What & Why
Before, a weighted call built two full copies of the data, one after the other.
_sumcomputedweights[:, None] * Xonly to take column sums._M2_densecomputed the same product again fornp.dot((weights[:, None] * X).T, Y). Each copy is as large as the input, but the returned moment matrix is only M x M.Now
_sumuses a vector-matrix product, which needs no copy._M2_denseweights one block of 1024 frames at a time in a reused buffer and adds each block product to the result. The math is the same sum in a different order, so results agree to floating-point rounding. Unweighted calls and thediag_onlypaths use the same code as before.diag_only=Trueremove_mean=TrueThe numbers are the peak of traced allocations (
tracemalloc) during one call, with the input allocated before. Withremove_mean=Truethe remaining 128 MB is the centered copy of the data, which this PR does not touch.Why this is safe and easy to maintain
The change is small: about 20 lines in
_sumand_M2_dense, with no new helper, argument or dependency. Return types, dtypes and shapes stay the same, and values differ only by floating-point rounding. The existing tests intests/covariancecompare weighted and unweighted moments with reference values and cover both functions.Validation
python -m pytest tests/covariance/test_moments.py -q --tb=short: 22 passed.python -m pytest tests/covariance -q --tb=short: 67 passed (_sumand_M2_denseare shared withmoments_XXXYandmoments_block).VmHWMover the 200000 x 80 weighted call, three fresh processes each): 128.4-128.6 MB before, 1.56 MB after.remove_mean=Trueit was 138-162 ms before and 148-211 ms after, so that case can be a little slower. Unweighted: 104 ms and 106 ms.rtol=1e-9for weighted, unweighted,remove_mean,column_selection,diag_only, sparse, boolean and float32 inputs.Reproduce the first table row:
PR checklist
tests/covariancetests cover the changed functions)AUTHORSAI assistance
This PR was prepared with AI assistance from Aiden. Candidates were tracked with Weco Observe.
Weco share link: https://dashboard.weco.ai/share/Gh2FBnhVjIAP-u5ARXToJoX_pjCWkTj6